<?

class ImageItemRenderer extends TRepeaterItemRenderer
{
	//public $serverPathDownload = "user_images/";
//	public $id = -1;
	
	public function onInit($param)
    {

		$this->VerifyOwner();
	}
	
	public function VerifyOwner()
	{
		if($this->getPage()->isNavigatedUserSameAsOwner())
		{ // showing owner profile...
		
			
		}			
		else // showing someone else profile
		{
			// hide delete
			$this->btnDeletePic->Visible = false;
		}
	}
	
	public function getImage()
	{
		$path = $this->Application->Parameters['ImageFolderName'] . "/" . $this->Data->user_id . "/cnt/" . $this->Data->ImagePath;
		//echo "PATH >> ". $path . "\n ";
		return $path;
	}
	
	public function btnDeletePic_click()
	{
		$id = $this->hidPicID->Value;
		$rec = ImageCntRecord::finder()->findById($id);
		
		// delete actual pic
		$this->deleteOldPic($rec->ImagePath);
		
		// delete records in DB
		ImageCntRecord::finder()->deleteByPk($id);
		
		
		$this->Response->reload();
	}
	
	public function deleteOldPic($old_path)
	{
		$id = $this->getPage()->id;
		if($old_path != null)
		{	
			//**** at server used >>>	$path = $this->Application->AssetManager->getBasePath() ."/images/". $this->id ."/";
			//$old_path = $this->Application->AssetManager->getBasePath() ."\\images\\". $this->id ."\\". $old_path;
			
			$old_path = $this->Application->getBasePath() . "\\..\\" . $this->Application->Parameters['ImageFolderName'] ."\\".$id."\\cnt\\".$old_path ;
			//die(is_file($old_path). " path: ".$old_path);

			// check the existence of file before delete it
			if(is_file($old_path))
			{
				$flgDelete = unlink($old_path);
				return $flgDelete;
			}
			

		}
		
		return false;
	}
}

?>